<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="企业信息管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	        <a-input v-model="queryForm.name" placeholder="请输入企业名称" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.industry" placeholder="请输入行业类型" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
	        <a-input v-model="queryForm.status" placeholder="请输入注册状态" allow-clear @change="search">
	          <template #prefix><icon-search /></template>
	        </a-input>
        <a-button @click="reset">重置</a-button>
      </template>
      <template #status="{ record }">
        <a-tag v-if="record.status === 0" color="green">已审核</a-tag>
        <a-tag v-if="record.status === 1" color="orange">未审核</a-tag>
        <a-tag v-if="record.status === 2" color="red">审核失败</a-tag>
      </template>
      <template #custom-right>
        <a-button v-permission="['jobfair:company:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <span>新增</span>
        </a-button>
        <a-tooltip content="导出">
          <a-button v-permission="['jobfair:company:export']" class="gi_hover_btn-border" @click="onExport">
            <template #icon>
              <icon-download />
            </template>
          </a-button>
        </a-tooltip>
      </template>
      <template #name="{ record }">
        <a-link @click="onDetail(record)">{{ record.name }}</a-link>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['jobfair:company:update']" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['jobfair:company:delete']"
            status="danger"
            :disabled="record.disabled"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <CompanyAddModal ref="CompanyAddModalRef" @save-success="search" />
    <CompanyDetailDrawer ref="CompanyDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import CompanyAddModal from './CompanyAddModal.vue'
import CompanyDetailDrawer from './CompanyDetailDrawer.vue'
import { type CompanyResp, type CompanyQuery, deleteCompany, exportCompany, listCompany } from '@/apis'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { useDict } from '@/hooks/app'

defineOptions({ name: 'Company' })

const queryForm = reactive<CompanyQuery>({
  name: undefined,
  industry: undefined,
  status: undefined,
  sort: ['createTime,desc']
})


const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listCompany({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: '企业名称', dataIndex: 'name', slotName: 'name' },
  { title: '行业类型', dataIndex: 'industry', slotName: 'industry' },
  { title: '公司地址', dataIndex: 'location', slotName: 'location' },
  { title: '网站地址', dataIndex: 'website', slotName: 'website' },
  { title: '注册状态', dataIndex: 'status', slotName: 'status' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['jobfair:company:update', 'jobfair:company:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  queryForm.industry = undefined
  queryForm.status = undefined
  search()
}

// 删除
const onDelete = (item: CompanyResp) => {
  return handleDelete(() => deleteCompany(item.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportCompany(queryForm))
}

const CompanyAddModalRef = ref<InstanceType<typeof CompanyAddModal>>()
// 新增
const onAdd = () => {
  CompanyAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (item: CompanyResp) => {
  CompanyAddModalRef.value?.onUpdate(item.id)
}

const CompanyDetailDrawerRef = ref<InstanceType<typeof CompanyDetailDrawer>>()
// 详情
const onDetail = (item: CompanyResp) => {
  CompanyDetailDrawerRef.value?.onDetail(item.id)
}
</script>

<style lang="scss" scoped></style>
